﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MISAPP.Classify.DataAccess;
using MISAPP.MISInterface;

namespace MISAPP.Classify.Service
{
    public class Service_CIB001
    {
        DA_CIB001 cib = null;
        protected DA_CIB001 CIB
        {
            get
            {
                if (cib == null)
                {
                    cib = DA_CIB001.GetInstance();
                }
                return cib;
            }
        }

        public DataSet GetCriteriaData()
        {
            return CIB.GetCriteria();
        }

        public string[] GetCriteriaArray()
        {
            string[] fldArray = CIB.GetDistinctField();
            List<string> lst = new List<string>();
            foreach(string str in fldArray)
            {
                lst.Add(str);
            }
            lst.Add("LNAC_ID");
            return lst.ToArray();
        }

        public DataTable GetGroupBy()
        {
            return CIB.GetGroupBy();
        }

        public DataTable GetSummaryData(System.Collections.ArrayList p_criteria)
        {
            DataTable dt = null;
            if (p_criteria.Count != 5)
            {
                throw new Exception();
            }
            try
            {
                List<iSelectList> criteriaList = p_criteria[0] as List<iSelectList>;
                CIB.ResetCriteria();
                foreach(SelectList SL in criteriaList)
                {
                    CIB.AddCriteria(SL.GetField(), CIB.GetInString(SL.GetReverse()), SL.GetListArray());
                }

                string p_groupBy = p_criteria[1].ToString();
                bool p_checkAccount = (bool)p_criteria[2];
                string unit = (string)p_criteria[3];
                string accno = (string)p_criteria[4];
                dt = CIB.GetSummaryData(p_groupBy, p_checkAccount,unit,accno);                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }

        public DataTable GetDetailData(System.Collections.ArrayList p_criteria)
        {
            //if (p_criteria.Count < 3)
            //{
            //    throw new Exception();
            //}
            DataTable dt = null;
            try
            {
                Dictionary<string, string> p_selectKey = p_criteria[0] as Dictionary<string, string>;
                List<iSelectList> criteriaList = p_criteria[1] as List<iSelectList>;
                bool p_checkAccount = (bool)p_criteria[2];

                CIB.ResetCriteria();
                foreach (SelectList SL in criteriaList)
                {
                    CIB.AddCriteria(SL.GetField(), CIB.GetInString(SL.GetReverse()), SL.GetListArray());
                }
                CIB.AddCriteria2(p_selectKey);
                string accno = string.Empty;
                if (p_criteria.Count >= 4)
                {
                    accno = (string)p_criteria[4];
                }
                
                dt = CIB.GetDetailData(p_checkAccount,accno);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }

    }
}
